<template>
    <el-dialog v-model="dialogVisible" title="选择设备" width="800px" :close-on-click-modal="false">
        <div class="device-selection">
            <div class="device-wrapper">
                <!-- 一级设备列表 -->
                <div class="device-category">
                    <div v-for="device in deviceList" :key="device.name" class="device-item"
                        :class="{ 'active': selectedCategory === device.name }" @click="selectCategory(device)">
                        <SvgIcon :icon="device.iconName" size="32px" class="device-icon" />
                        <span class="device-name">{{ device.label }}</span>
                    </div>
                </div>

                <!-- 二级设备列表 -->
                <div class="device-list" v-if="selectedCategory">
                    <div v-for="subDevice in currentSubDevices" :key="subDevice.id" class="sub-device-item"
                        @click="selectDevice(subDevice)">
                        <span class="sub-device-name">{{ subDevice.name }}</span>
                    </div>
                </div>
            </div>
        </div>
    </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue';
import SvgIcon from '@/components/icon/SvgIcon.vue';

const props = defineProps({
    visible: Boolean,
    deviceList: Array
});

const emits = defineEmits(['update:visible', 'select-device']);

const dialogVisible = computed({
    get: () => props.visible,
    set: (value) => emits('update:visible', value)
});

const selectedCategory = ref(null);

const currentSubDevices = computed(() => {
    if (!selectedCategory.value) return [];
    const category = props.deviceList.find(d => d.name === selectedCategory.value);
    return category ? category.children : [];
});

const selectCategory = (device) => {
    selectedCategory.value = device.name;
};

const selectDevice = (subDevice) => {
    dialogVisible.value = false;
    emits('select-device', subDevice);
};
</script>

<style scoped>
.device-selection {
    display: flex;
    min-height: 400px;
}

.device-wrapper {
    display: flex;
}

.device-category {
    width: 200px;
    border-right: 1px solid var(--el-border-color);
    padding: var(--gap-normal);
    overflow-y: auto;
}

.device-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: var(--gap-small);
    border: 1px solid var(--el-border-color);
    border-radius: var(--el-border-radius-base);
    cursor: pointer;
    margin-bottom: var(--gap-small);
    transition: all 0.3s;
}

.device-item:hover {
    border-color: var(--el-color-primary);
    background-color: var(--el-color-primary-light-9);
}

.device-item.active {
    background-color: var(--el-color-primary-light-8);
    border-color: var(--el-color-primary);
}

.device-list {
    flex: 1;
    padding: var(--gap-normal);
    overflow-y: auto;
}

.sub-device-item {
    padding: var(--gap-small) var(--gap-normal);
    border: 1px solid var(--el-border-color);
    border-radius: var(--el-border-radius-base);
    margin-bottom: var(--gap-small);
    cursor: pointer;
    transition: all 0.3s;
}

.sub-device-item:hover {
    background-color: var(--el-color-primary-light-9);
    border-color: var(--el-color-primary);
}

.device-name {
    margin-top: var(--gap-small);
    font-size: var(--el-font-size-small);
    color: var(--el-text-color-regular);
}
</style>